package com.hzg.ssm.service.impl;

import com.hzg.ssm.domain.Employee;
import com.hzg.ssm.mapper.EmployeeMapper;
import com.hzg.ssm.query.PageResult;
import com.hzg.ssm.query.QueryObject;
import com.hzg.ssm.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ Description: TODO
 * @ Author: 卓哥
 * @ Date: 2020/7/5 18:45
 * @ Version: 1.0
 */
@Service
public class EmployeeServiceImpl implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public void delete(Long id) {
        employeeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void insert(Employee employee) {
        employeeMapper.insert(employee);

    }

    @Override
    public Employee select(Long id) {
        return employeeMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Employee> selectAll() {

        return employeeMapper.selectAll();
    }

    @Override
    public void update(Employee employee) {
        employeeMapper.updateByPrimaryKey(employee);

    }

    @Override
    public PageResult<Employee> query(QueryObject queryObject) {
        int totalCount = employeeMapper.queryCount(queryObject);
        int currentPage = queryObject.getCurrentPage();
        int pageSize = queryObject.getPageSize();
        if (totalCount <= 0) {
            return PageResult.empty(pageSize);

        }
        //
        int totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;
        //
        if (currentPage <= 1) {
            queryObject.setCurrentPage(1);

        }
        if (currentPage >= totalPage) {
            queryObject.setCurrentPage(totalPage);
            System.out.println("我擦");

            System.out.println("罗坤论你妹的");
        }
        return new PageResult(queryObject.getCurrentPage(), queryObject.getPageSize(), totalCount, employeeMapper.queryList(queryObject));
            
    }
}
